import Vue from "vue"
import VueRouter from "vue-router"
import store from "../store"
Vue.use(VueRouter)
// 解决vue-router的一个bug（跳转当前页面会出现bug）
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location){
    return originalPush.call(this,location).catch(err=>err)
}

const router = new VueRouter({
    routes:[
        {
            path:"/",
            component: () => import("../components/pages/Layout.vue"),
            redirect:"/home",
            meta:{requiresAuth:true},
            children:[
                {
                    path: "/home",
                    component: ()=> import("../components/pages/home/Index")
                },
                //菜单管理
                {
                    path: "/menu",
                    component: ()=> import("../components/pages/menu/Index")
                },
                {
                    path: "/menu/add",
                    component: ()=> import("../components/pages/menu/Edit")
                },
                {
                    path: "/menu/:id",
                    component: ()=> import("../components/pages/menu/Edit")
                },
                //角色管理
                {
                    path: "/role",
                    component: ()=> import("../components/pages/role/Index")
                },
                {
                    path: "/role/add",
                    component: ()=> import("../components/pages/role/Edit")
                },
                {
                    path: "/role/:id",
                    component: ()=> import("../components/pages/role/Edit")
                },
                //管理员管理
                {
                    path: "/admin",
                    component: ()=> import("../components/pages/admin/Index")
                },
                {
                    path: "/admin/add",
                    component: ()=> import("../components/pages/admin/Edit")
                },
                {
                    path: "/admin/:uid",
                    component: ()=> import("../components/pages/admin/Edit")
                },
                //分类管理
                {
                    path: "/cate",
                    component: ()=> import("../components/pages/cate/Index")
                },
                {
                    path: "/cate/add",
                    component: ()=> import("../components/pages/cate/Edit")
                },
                {
                    path: "/cate/:id",
                    component: ()=> import("../components/pages/cate/Edit")
                },
                //规格管理
                {
                    path: "/specs",
                    component: ()=> import("../components/pages/specs/Index")
                },
                {
                    path: "/specs/add",
                    component: ()=> import("../components/pages/specs/Edit")
                },
                {
                    path: "/specs/:id",
                    component: ()=> import("../components/pages/specs/Edit")
                },
                //商品管理
                {
                    path: "/goods",
                    component: ()=> import("../components/pages/goods/Index")
                },
                {
                    path: "/goods/add",
                    component: ()=> import("../components/pages/goods/Edit")
                },
                {
                    path: "/goods/:id",
                    component: ()=> import("../components/pages/goods/Edit")
                },
            ]
        },
        {
            path:"/login",
            component:()=>import("../components/pages/Login.vue")
        }
    ]
})
//导航守卫--全局前置守卫，验证是否登录
router.beforeEach((to,from,next)=>{
    // 判断是否需要登录
    if(to.matched.some(recored => recored.meta.requiresAuth)){
        // 判断是否已经登录
        if(store.state.userInfo.token){
            next()
        }else{
            next({
                path:"/login",
                query:{redirect: to.fullPath}
            })
        }
    }else{
        next();
    }
})
export default router;